package yunquan.wang.modules.customer.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.dao.springdata.BaseRepository;
import yunquan.wang.base.service.impl.BaseServiceImpl;
import yunquan.wang.modules.customer.dao.ContactsDao;
import yunquan.wang.modules.customer.dao.ContactsInfoDao;
import yunquan.wang.modules.customer.dao.ContactsVisitInfoDao;
import yunquan.wang.modules.customer.entity.Contacts;
import yunquan.wang.modules.customer.entity.ContactsVisitInfo;
import yunquan.wang.modules.customer.service.ContactsService;

@Service("contactsService")
@Transactional(value = "jpaTransaction")
public class ContactsServiceImpl extends BaseServiceImpl<Contacts> implements
		ContactsService {

	@Resource
	private ContactsDao contactsDao;
	@Resource
	private ContactsInfoDao contactsInfoDao;
	@Resource
	private ContactsVisitInfoDao contactsVisitInfoDao;

	@Override
	public BaseRepository<Contacts, String> getDao() {
		return contactsDao;
	}

	public Contacts save(Contacts entity) {
		if (entity.isNew()) {
			entity.setContactsInfo(contactsInfoDao.save(entity
					.getContactsInfo()));
		}

		entity = contactsDao.saveAndFlush(entity);

		return entity;
	}

	public Contacts get(String id) {
		Contacts entity = contactsDao.findOne(id);
		Hibernate.initialize(entity.getContactsInfo());
		return entity;
	}

	@Override
	public Page<ContactsVisitInfo> findVistPage(Pageable page,
			List<PropertyFilter> filters) {
		return null;
	}

	@Override
	public ContactsVisitInfo saveVisit(ContactsVisitInfo entity) {
		return contactsVisitInfoDao.save(entity);
	}

	@Override
	public ContactsVisitInfo getVist(String id) {
		return contactsVisitInfoDao.findOne(id);
	}

	@Override
	public void deleteVisit(String ids) {
		contactsVisitInfoDao.delete(ids);
	}
}
